package com.jrx.anytxn.customer.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.customer.dto.customer.CustomerRes;
import com.jrx.anytxn.customer.dto.customer.CustomerSecondLevelRes;
import com.jrx.anytxn.customer.entity.TlCouponDetail;
import com.jrx.anytxn.customer.mapper.ext.ExtTlCouponDetailMapper;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.customer.service.ITlCouponDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class TlCouponDetailServiceImpl implements ITlCouponDetailService {

    private static final Logger logger = LoggerFactory.getLogger(TlCouponDetailServiceImpl.class);

    @Autowired
    private ExtTlCouponDetailMapper extTlCouponDetailMapper;
    @Autowired
    private ICustomerService customerService;

    @Override
    public List<TlCouponDetail> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId, String chineseName, String customerId, String idNumber, String updateBy, String mobile, String beginDate, String endDate) throws TxnException {
        Integer startIndex=(pageNum-1)*(pageSize);
        List<TlCouponDetail> tlCouponDetailList = extTlCouponDetailMapper.selectAllByPageAndTenId(tenantId, chineseName,customerId,idNumber,updateBy, mobile,beginDate,endDate,startIndex,pageSize);
        return tlCouponDetailList;
    }

    @Override
    public int countAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId, String chineseName, String customerId, String idNumber, String updateBy, String mobile, String beginDate, String endDate) throws TxnException {
        Integer selectCount=extTlCouponDetailMapper.selectCount(tenantId, chineseName,customerId,idNumber,updateBy, mobile,beginDate,endDate);
        return selectCount;
    }

    @Override
    public void addCouponDetail(List<TlCouponDetail> tlCouponDetailList) {
        extTlCouponDetailMapper.batchInsert(tlCouponDetailList);
    }

    /**
     * 根根 客户id、活动编号、权益id、租户号 查询 权益发放明细表
     *
     * @param customerId 客户id
     * @param activityId 客户id
     * @param rightsId   权益id
     * @param tenantId   租户号
     * @return
     */
    @Override
    public List<TlCouponDetail> findByCustomerIdAndActivityIdAndRightsId(String customerId, String activityId, String rightsId, String tenantId) {
        return extTlCouponDetailMapper.selectByCustomerIdAndActivityIdAndRightsId(customerId,activityId,rightsId,tenantId);
    }

    @Override
    public List<TlCouponDetail> findByUserIdAndActivityId(String userId, String activityId, String tenantId,String channel) throws TxnBizException {
        CustomerRes customerRes = customerService.getCustomerByChannelAndUserId(channel, userId, tenantId);
        if (customerRes == null) {
            logger.error("根据channel、userid、tenantId 查询一级表，二级表信息，查无记录");
            throw new TxnBizException(TxnRespCode.NO_DATA);
        }
        CustomerSecondLevelRes customerSecondLevelRes = customerRes.getSecondLevel();
        return extTlCouponDetailMapper.selectByCustomerIdAndActivityIdAndRightsId(customerSecondLevelRes.getCustomerId(),activityId,null,tenantId);
    }

    /**
     * 根根 客户id、活动编号、权益id、租户号、时间范围内 查询 权益发放明细表
     *
     * @param customerId
     * @param activityId
     * @param rightsId
     * @param tenantId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<TlCouponDetail> findByCustomerIdAndActivityIdAndRightsIdWithPeriod(String customerId, String activityId, String rightsId, String tenantId, String beginTime, String endTime) {
        return extTlCouponDetailMapper.selectByCustomerIdAndActivityIdAndRightsIdWithPeriod(customerId, activityId, rightsId, tenantId, beginTime, endTime);
    }

    /**
     * 根根 客户id、活动编号、租户号、时间范围内 查询 权益发放明细表
     *
     * @param customerId
     * @param activityId
     * @param tenantId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<TlCouponDetail> findByCustomerIdAndActivityIdWithPeriod(String customerId, String activityId, String tenantId, Date beginTime, Date endTime) {
        return extTlCouponDetailMapper.selectByCustomerIdAndActivityIdWithPeriod(customerId, activityId, tenantId, beginTime, endTime);
    }

    @Override
    public List<TlCouponDetail> selectByCustomerIdAndActivityIdAndRightsIdAndRightsType(String customerId, String activityId, String rightsId, String tenantId, String rightsType) {
        return extTlCouponDetailMapper.selectByCustomerIdAndActivityIdAndRightsIdAndRightsType(customerId,activityId,rightsId,tenantId,rightsType);
    }

    /**
     * 根根 客户id、创建日期、权益id、租户号 查询 权益发放明细表
     *
     * @param customerId
     * @param rightsId
     * @param tenantId
     * @param date
     * @return
     */
    @Override
    public List<TlCouponDetail> listByCustomerIdAndRightsIdAndCreateDate(String customerId, String rightsId, String tenantId, Date date) {
        return null;
    }

    @Override
    public int insert(TlCouponDetail couponDetail) {
        return extTlCouponDetailMapper.insert(couponDetail);
    }

    @Override
    public int insertBatch(List<TlCouponDetail> couponDetailList) {
        return extTlCouponDetailMapper.batchInsert(couponDetailList);
    }

    /**
     * 根据租户号、客户id、活动id、被推荐人非空 查询记录
     *
     * @param mobile
     * @param activityId
     * @param tenantId
     * @return
     */
    @Override
    public List<TlCouponDetail> selectByMobileAndActivityIdAndRecommendedNonNull(String mobile, String activityId, String tenantId) {
        return extTlCouponDetailMapper.selectByMobileAndActivityIdAndRecommendedNonNull(mobile,activityId,tenantId);
    }


}
